package com.twd.aio;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

class FutureTaskTest implements Callable<String> {
    ll<String> ll;

    public FutureTaskTest(ll<String> ll) {
        this.ll = ll;
    }

    @Override
    public String call() throws ExecutionException, InterruptedException {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("===========================");

        ll.executor();
        return "pppppp";
    }
}

class ll<V> extends FutureTask<V> {
    static an o = null;

    public ll(Callable<V> callable) {
        super(callable);
    }

    public ll(Runnable runnable, V result) {
        super(runnable, result);
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        DefaultCall defaultCall = new DefaultCall();

        Default<String> stringDefault = new Default<>();
        Default<String> stringDefault1 = new Default<>(stringDefault, new DefaultCall());
        if (stringDefault1.isDone()) {
            System.out.println(stringDefault1.get()); //线程二：阻塞获取结果
            System.out.println("normal");

        } else {
            stringDefault1.addlisterner(() -> {
                System.out.println(stringDefault1.get()); //线程二：阻塞获取结果
                System.out.println("listenr");
            });

        }

    }

    public void addlisterner(an a) {
        //添加监听器
        o = a;
    }

    public void executor() throws ExecutionException, InterruptedException {
        o.listener();
    }
}

interface an {
    public void listener() throws ExecutionException, InterruptedException;
}


class Default<T> {
    FutureTask<T> futureTask;

    public Default() {

    }

    public Default(Default def, DefaultCall defaultCall) {
        defaultCall.setDe(def);
        def.setFutureTask(new FutureTask(defaultCall));
        setFutureTask(def.getFutureTask());
        start();
    }

    public void setFutureTask(FutureTask<T> task) {
        futureTask = task;
    }
//    public void setTask(Callable<String> callable){
//
//    }

    public boolean isDone() {
        return futureTask.isDone();
    }

    public FutureTask<T> getFutureTask() {
        return futureTask;
    }

    public void start() {
        new Thread(futureTask).start();
    }

    public T get() {
        try {
            return futureTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

    static an o = null;

    public void addlisterner(an a) {
        //添加监听器
        o = a;
    }

    public void executor() throws ExecutionException, InterruptedException {
        o.listener();
    }

}

class DefaultCall implements Callable {
    Default<?> de;

    public void setDe(Default<?> de) {
        this.de = de;
    }

    @Override
    public String call() throws Exception {
        TimeUnit.SECONDS.sleep(5);
        System.out.println("====");
        new Thread(()-> {
            try {
                de.executor();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        return "ssss";
    }
}
